package com.yunao.commons.metasearch.ms;

import com.yunao.commons.enums.SearchTypeEnum;
import com.yunao.commons.form.MtForm;
import com.yunao.commons.metasearch.common.Convert;
import com.yunao.commons.metasearch.common.DateUtil;
import com.yunao.commons.metasearch.common.StringUtil;
import com.yunao.commons.metasearch.entity.CatchResult;
import com.yunao.commons.metasearch.entity.CatchResults;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MsThread implements Callable<CatchResults> {

	private String source;
	private MtForm mt;
	private Sina sina=new Sina();


	public MsThread(MtForm mt, String source) {
		this.source = source;
		this.mt = mt;
	}

	public CatchResults call() throws Exception {
		//剔除不能搜索时间范围的搜索


		String type = mt.getType();
		//System.out.println("type:  "+type);
		if (type == null || type.length() == 0)
			return null;
		// 类型 map

		Map<String, T> tMap = R.mspf.get(mt.getType());
		//System.out.println("tmap:  "+tMap);
		if (tMap == null)
			return null;

		T t = tMap.get(source);
		if (t == null)
			return null;
		// 创建 url
		CatchResults catchResults=null;
//		System.setProperty("http.proxySet",t.set);
//		System.setProperty("http.proxyHost", t.ip);
//		System.setProperty("http.proxyPort", t.port);
//		System.setProperty("http.proxyType", t.type);
		//	System.out.println("url0:  "+url);
		String url = this.curl(mt, t);
		if(mt.getRange()!=null||mt.getRange().trim()!=""){
			if(SearchTypeEnum.MS_NEWS.getSearchType().equals(type)){
				if(source.equals("qihu")){
					catchResults =new CatchResults();
					catchResults.setTotal(0);
					return catchResults;
				}
			}
			if(SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(type)){
				if(source.equals("chinaso")){
					catchResults =new CatchResults();
					catchResults.setTotal(0);
					return catchResults;
				}
			}
			if(SearchTypeEnum.MS_TIEBA.getSearchType().equals(type)){
				if(source.equals("baidu")){
					catchResults =new CatchResults();
					catchResults.setTotal(0);
					return catchResults;
				}
			}
			if(SearchTypeEnum.MS_FORUM.getSearchType().equals(type)){
				if(source.equals("tianya")){
					catchResults =new CatchResults();
					catchResults.setTotal(0);
					return catchResults;
				}
			}
		}
		catchResults = this.parse(url, t);

		return catchResults;
	}

	/**
	 * 生成 url
	 */
	private String curl(MtForm mt, T t) {
		String url = null;
		String word = null;
		String type = null;
		try {
			url = t.url_base;
			//System.out.println("url0:"+url);

			// 关键词编码
			//System.out.println("url0.0:"+url);

			word = mt.getWord();
			//System.out.println("全网监测运算符处理"+word);
			if(source!=null&&mt.getType()!=null&&!mt.getType().equals("weibo")){
				String range = mt.getRange();
				String[] split = t.url_range.split(",");
				System.out.println("name："+t.name);
				System.out.println("split.length："+split.length);
				if( range.length()>=1 && range.equals("d")){
					url = url+"&"+split[0];
					System.out.println("split[0]："+split[0]);
				}else if(range.length()>=2 && range.equals("w")){
					url = url+"&"+split[1];
					System.out.println("split[1]："+split[1]);
				}else if(split.length>=3 && range.equals("m")){
					url = url+"&"+split[2];
					System.out.println("split[2]："+split[2]);
				}
			}
			if(source!=null&&mt.getType()!=null&&source.equals("baidu")&&mt.getType().equals("news"))
			{
				word=word.replace("+", " ");
				word=word.replace("|", " |");
				word=word.replace("-", " -");
				System.out.println("baidu news :"+word);

			}
			if(source!=null&&mt.getType()!=null&&source.equals("soso")&&mt.getType().equals("news"))
			{
				word=word.replace("+", " ");
				word=word.replace("|", " ");
				word=word.replace("-", " -");
				//word=word.replace("(", " ");
				//word=word.replace(")", " ");
			}

			if(source!=null&&mt.getType()!=null&&mt.getType().equals("weibo"))
			{
				word=word.replace("+", " ");
				word=word.replace("|", "~");
				word=word.replace("-", " -");
				//word=word.replace("(", " ");
				//word=word.replace(")", " ");

				if(mt.getRange()==null) {
					//使用时间戳 yyyy-MM-dd
					SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT);
					SimpleDateFormat sdf2 = new SimpleDateFormat(DateUtil.WEIBO_DATE_FORMAT);
					if (mt.getBegin() == null) {
						String begin = sdf2.format(new Date(System.currentTimeMillis()));
						begin = begin + "-0";//2019-08-29-0
						mt.setBegin(begin);
					} else {
						String begin = sdf2.format(new Date(mt.getBegin()));
	//					begin = begin + "-0";
						mt.setBegin(begin);
					}

					if (mt.getEnd() == null) {
						String end = sdf.format(new Date(System.currentTimeMillis()));
						end = end + "-23";//2019-08-29-23
						mt.setEnd(end);
					} else {
						String end = sdf2.format(new Date(mt.getEnd()));
	//					end = end + "-0";
						mt.setEnd(end);
					}
				}else{
					//使用时间段
					SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.WEIBO_DATE_FORMAT);
					String range = mt.getRange();
					String end = sdf.format(new Date(System.currentTimeMillis()));
					mt.setEnd(end);
					if ("d".equalsIgnoreCase(range)){

						String begin = sdf.format(new Date(System.currentTimeMillis()-24*60*60*1000));
						mt.setBegin(begin);//日

					}
					else if ("w".equalsIgnoreCase(range)){

						String begin = sdf.format(new Date(System.currentTimeMillis()-7*24*60*60*1000));
						mt.setBegin(begin);//周

					}else if ("m".equalsIgnoreCase(range)){

						String begin = sdf.format(new Date(System.currentTimeMillis()-30*24*60*60*1000));
						mt.setBegin(begin);//月

					}

				}



			}

			if(source!=null&&mt.getType()!=null&&source.equals("baidu")&&mt.getType().equals("tieba"))
			{
				word=word.replace("+", " ");
				word=word.replace("|", " ");
				word=word.replace("-", " ");
				//word=word.replace("(", " ");
				//word=word.replace(")", " ");
			}

			if(source!=null&&mt.getType()!=null&&source.equals("baidu")&&mt.getType().equals("wenda"))
			{
				word=word.replace("+", " ");
				//word=word.replace("|", " ");
				word=word.replace("-", " -");
				//	word=word.replace("(", " ");
				//	word=word.replace(")", " ");
			}
			if(source!=null&&mt.getType()!=null&&source.equals("soso")&&mt.getType().equals("weixin"))
			{
				word=word.replace("+", " ");
				//word=word.replace("|", " ");
				word=word.replace("-", " -");
				//word=word.replace("(", " ");
				//word=word.replace(")", " ");
			}
			if(source!=null&&mt.getType()!=null&&source.equals("tianya")&&mt.getType().equals("forum"))
			{
				word=word.replace("+", " ");
				//word=word.replace("|", " ");
				word=word.replace("-", " -");
				//word=word.replace("(", " ");
				//word=word.replace(")", " ");
			}
			type = mt.getType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (word != null && word.length() > 0 && t.url_word_encode != null && t.url_word_encode.length() > 0) {
			try {
				word = (t.url_word_encode == null || "unencode".equals(t.url_word_encode)) ? word : URLEncoder.encode(
						word, t.url_word_encode);
				url = url.replace("%word%", word);
			} catch (Exception e) {
				return null;
			}
		}
		// 页数
		int page = mt.getPage();
		//System.out.println("url0.1:"+url);

		if (page > 1 && t.url_page != null && t.url_page.length() > 0 && !type.equalsIgnoreCase("weibo")) {
			try {
				// 页数计算公式
				String formula = t.url_page.replace("%page%", page + "");
				url += "&" + formula.substring(0, formula.indexOf("=") + 1)
						+ new StringUtil().parse(formula.substring(formula.indexOf("=") + 1)).toString();
			} catch (Exception e) {

			}
		}
		// 时间设定
		//System.out.println("url0.2:"+url);

		String begin = mt.getBegin(), end = mt.getEnd();
//		System.out.print(mt.getBegin()+"ooooooooooooooooo"+mt.getEnd());
		if (type.equalsIgnoreCase("weibo")) {
			try {
				if (begin != null && begin.length() > 0 && end != null && end.length() > 0) {
					// 微博时间
					url = url.replace("%begin%",begin);
					url = url.replace("%end%",end);
				}
			} catch (Exception e) {

			}
		}
		// 排序，时间和相关度
		//System.out.println("url0.3:"+url);

		String sort = mt.getSort();
		if(sort==null||sort.equals(""))
			sort="t";
		if (sort != null && sort.length() > 0 && t.url_sort != null && t.url_sort.length() > 0&&!source.equalsIgnoreCase("weibo")) {
			try {
				String[] strs = Convert.split(t.url_sort, ",");
				if ("r".equals(sort))
					url += "&" + strs[0];
				else if ("t".equals(sort))
					url += "&" + strs[1];
			} catch (Exception e) {

			}
		}
		// 全量查询与精量查询
		//	System.out.println("url0.4:"+url);
		if ("fq".equals(mt.getDup()) && t.url_dup != null && t.url_dup.length() > 0&&!source.equalsIgnoreCase("weibo"))
			url += "&" + t.url_dup;


		// 微博类型，不限制，图片，视频，音乐
		String media = mt.getMedia();
		//System.out.println("url0.5:"+url);

		if (media != null && media.length() > 0 && t.url_mediatype!= null && t.url_mediatype.length() > 0&&!source.equalsIgnoreCase("weibo")) {
			try {
				//System.out.println("media:"+media);

				String[] strs = Convert.split(t.url_mediatype, ",");
				url+="&"+strs[Integer.parseInt(media)];
			} catch (Exception e) {

			}
		}
		//System.out.println("url1:"+url);
		return url;
	}

	/**
	 * 解析 html
	 */
	private CatchResults parse(String url, T t) throws MalformedURLException, IOException {
		//System.out.println("parsing");
		System.out.println("url: "+url);
		// html 连接、解析器
		JsoupUtil jsoup = new JsoupUtil();
		Document doc = null;

		//问答cookie
		Map<String,String> map = new HashMap<String,String>();
		String cookie = "ww_sTitle=12306*%E6%88%91; CXID=3E2F52FC1B1C956A64ED86459842404C; SUV=1490261629694267; ssuid=9567856990; sw_uuid=8728185958; sg_uuid=9031498268; pgv_pvi=6997366784; _ga=GA1.2.559777949.1490261636; wuid=AAEEtqhfGAAAAAqLEm9HzwMAGwY=; ad=siRNnkllll2Bl9SalllllV6GplDllllllbFypyllll9lllllxv7ll5@@@@@@@@@@; SUID=19D0B4013765860A58CF8318000975A9; sct=25; SNUID=012F2E7D0D085C213C88ACC70D94FD27; usid=2DD0B4014F0A900A000000005923E8F7; dt_ssuid=7928586704; IPLOC=CN1100; ld=Qlllllllll2BwM0TlllllV66cj6llllltjGARlllll9lllllpllll5@@@@@@@@@@";
		String[] cookiess=cookie.split("; ");
		for(int  j=0;j<cookiess.length;j++){
			try{
				map.put(cookiess[j].split("=")[0],cookiess[j].split("=")[1]);
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		//微信cookie
		Map<String,String> weixinMap = new HashMap<String,String>();
		String weixinCookie = "CXID=3E2F52FC1B1C956A64ED86459842404C; SUV=1490261629694267; ssuid=9567856990; sw_uuid=8728185958; sg_uuid=9031498268; pgv_pvi=6997366784; _ga=GA1.2.559777949.1490261636; wuid=AAEEtqhfGAAAAAqLEm9HzwMAGwY=; weixinIndexVisited=1; usid=2DD0B4014F0A900A000000005923E8F7; dt_ssuid=7928586704; SUID=19D0B4013765860A58CF8318000975A9; ad=SBRNnkllll2Bl9SalllllV6TmolllllllbFypyllll9lllllxh7ll5@@@@@@@@@@; ABTEST=0|1496974787|v1; SNUID=679BFE4B4B4F180CEA245E7A4B5A488B; ld=mlllllllll2BwM0TlllllVO4G$wllllltjGARlllll9lllll9Zlll5@@@@@@@@@@; JSESSIONID=aaaNjfRpbQlx-e0CsRIXv; sct=31; IPLOC=CN1310";
		String[] weixinCookies = weixinCookie.split("; ");
		for(int i=0;i<weixinCookies.length;i++){
			try{
				weixinMap.put(weixinCookies[i].split("=")[0], weixinCookies[i].split("=")[1]);
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(mt.getType())) {
			doc = JsoupUtil.conn(url);
		}
		else if(SearchTypeEnum.MS_WEIXIN.getSearchType().equals(mt.getType())){
			doc = JsoupUtil.conn(url, weixinMap);
		}
		else if(source.equals("sogou")){
			Map<String,String> cooMap = new HashMap<String,String>();
			String coo = "SUV=00543D8C7567810E5BD661C7A40F7507;  ssuid=8380916428; CXID=203E6905023B25D08DBDC2C20220F733;  pgv_pvi=6812549120; IPLOC=CN1100; QIDIANID=xAEKlcGkaDtT8x/7pq7+25sh7moXbsXcxygXB8BKhM0Qe8504FIAMLRwQ8jPahEd; sw_uuid=5042951016; SNUID=1532FD3F4C49DA68095244A54C3B9D83; ad=xZllllllll2N9wz4lllllVCuppkllllltEBiQlllllwlllll4Vxlw@@@@@@@@@@@; SUID=1E8167753921940A000000005AA73B02; YYID=01E5F310036DA218FA268042854AF807; newsCity=%u5317%u4EAC; usid=FTksp-bxUoaUGy80; pgv_si=s4832043008; sct=47; sg_uuid=6147112062; ld=Uyllllllll2NjhHxlllllVCIlLtllllltEBiQlllllZlllll4klll5@@@@@@@@@@";
			String[] coos = coo.split("; ");
			for(int i=0;i<coos.length;i++){
				try{
					cooMap.put(coos[i].split("=")[0], coos[i].split("=")[1]);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			doc = JsoupUtil.conn(url,cooMap);

		}else if(source.equals("chinaso")&&mt.getType().equals(SearchTypeEnum.MS_NEWS.getSearchType())){
			Map<String,String> cooMap = new HashMap<String,String>();
			String coo = "uid=CgqATl1pA386KmWFA0nIAg==; wdcid=3cc46f8933773493; Hm_lvt_91fa1aefc990a9fc21c08506e5983ddf=1567163265,1567394322; Hm_lpvt_91fa1aefc990a9fc21c08506e5983ddf=1567394322; cookie_name=115.182.121.88.1567394632692189; wdlast=1567394873";
			String[] coos = coo.split("; ");
			for(int i=0;i<coos.length;i++){
				try{
					cooMap.put(coos[i].split("=")[0], coos[i].split("=")[1]);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			doc = JsoupUtil.conn(url,cooMap);
		}else if(source.equals("bing")){
			Map<String,String> cooMap = new HashMap<String,String>();
			String coo = "_EDGE_V=1; MUID=25F33CD9A59B6ED327BF3112A4B56FD7; SNRHOP=I=&TS=; SRCHD=AF=NOFORM; SRCHUID=V=2&GUID=E2886316D13244DAA17F4CB4D83A086E&dmnchg=1; MUIDB=25F33CD9A59B6ED327BF3112A4B56FD7; ipv6=hit=1567415787432&t=4; _EDGE_S=mkt=zh-cn&F=1&SID=2B13F58797F1656427BCF84C96DF64AA; SRCHUSR=DOB=20190902&T=1567412195000; _SS=SID=2B13F58797F1656427BCF84C96DF64AA&HV=1567412475&bIm=885; SRCHHPGUSR=CW=1920&CH=522&DPR=1&UTC=480&WTS=63703008986";
			String[] coos = coo.split("; ");
			for(int i=0;i<coos.length;i++){
				try{
					cooMap.put(coos[i].split("=")[0], coos[i].split("=")[1]);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			doc = JsoupUtil.conn(url,cooMap);
		}
		else {
			doc = JsoupUtil.conn(url);
		}
//		System.out.println("doc:"+doc.html());

		if (doc == null)
			return null;
		// 解析结果
		CatchResults catchResults = new CatchResults();
		// 单元
		Elements elements = jsoup.list(doc, t.rst_all, t.rst_list);
		//	System.out.println("size0:"+elements.size());
		if (elements == null)
			return catchResults;
		// 找到 xxx 条结果
		int total=0;
		if(!mt.getType().equals("weibo"))
			total = jsoup.result(doc, t.rst_rst);
		System.out.println("total:"+total);
		catchResults.setTotal(total);
		// 当前时间点
		long ctm = System.currentTimeMillis();
		/*
		 * 解析各个单元
		 */
		//System.out.println("total:"+t.rst_all);
		for (Element element : elements) {

			try {
				CatchResult catchResult = new CatchResult();
				catchResult.setName(t.name);
				if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(mt.getType())) {
					// 转载节点
					Element resvNode = jsoup.parse(element, t.rst_reserved);
					// 转载节点存在且有内容
					if (resvNode != null && resvNode.hasText() && resvNode.text().length() > 0) {
						// 转载内容
						CatchResult rer = new CatchResult();
						// 微博正文
						rer.setSummary(jsoup.html(resvNode, t.rst_reserved_summary));
						// 发布时间
						String pubdate = jsoup.text(resvNode, t.rst_reserved_pubdate);
						parseDate(rer, ctm, pubdate);

						// 链接
						rer.setHref(jsoup.text(resvNode, t.rst_reserved_href));
						// 来源
						String source = jsoup.text(resvNode, t.rst_reserved_source);
						parseSource(rer, source);
						// 转播
						String repeat = jsoup.text(resvNode, t.rst_reserved_repeat);
						rer.setRepeat(repeat.replace("转发","").trim());
						// 评论
						String reply = jsoup.text(resvNode, t.rst_reserved_reply);
						rer.setReply(reply.replace("评论","").trim());
						// 媒体信息
						String media = jsoup.html(resvNode, t.rst_reserved_media);
						parseMedia(rer, t.name, media);
						resvNode.remove();
						catchResult.setRer(rer);
					}
				}
				// 标题 (非微博)
				String title = jsoup.html(element, t.rst_title);

				if (!mt.getType().equals(SearchTypeEnum.MS_WEIBO.getSearchType()))
					title = title.replaceAll("<[a-zA-Z0-9 =#\"]*>", "<em>").replaceAll("</[a-zA-Z0-9 =#\"]*>", "</em>");
				catchResult.setTitle(title);

				// 图片 (非微博)
				String img = jsoup.text(element, t.rst_img);
				catchResult.setImg(img);
				// 更多 (非微博)
				String more = jsoup.text(element, t.rst_more);
				catchResult.setMore(more);
				catchResult.setLinks(jsoup.text(element, t.rst_links));
				// 信息 (非微博)
				String info = jsoup.text(element, t.rst_info);

				if(this.source.equals(SearchTypeEnum.MS_FORUM_TIANYA.getSearchType()) )
				{
					info=jsoup.outhtml(element, t.rst_info);
					//System.out.println("test://info3/"+info);

				}


				/*if(mt.getType().equals(R.MS_WEIXIN))
				{
					info=jsoup.html(element, t.CatchResult_info);
					int index1=info.indexOf("\"");
					int index2=info.lastIndexOf("\"");
					info=info.substring(index1+1,index2);
					//System.out.println("test://info3/"+info);


				}*/







				catchResult.setInfo(info);
				// 作者名称
				catchResult.setAuthor(jsoup.text(element, t.rst_author));
				// System.out.println("author:"+jsoup.text(element, t.CatchResult_author));







				// 作者头像
				parseAvatar(catchResult, jsoup.text(element, t.rst_avatar));
				// 微博时作者链接，非微博时链接

				// 微博正文
				String summary = jsoup.html(element, t.rst_summary);

				if(mt.getType().equalsIgnoreCase(SearchTypeEnum.MS_NEWS.getSearchType())&&source.equalsIgnoreCase(SearchTypeEnum.MS_NEWS_BAIDU.getSearchType())) {
					summary = summary.split("<span")[0];
				}
//				System.out.println("0000000000:"+summary);
				if (!mt.getType().equals(SearchTypeEnum.MS_WEIBO.getSearchType())) {
					summary = summary.replaceAll("<[a-zA-Z0-9 =#\"]*>", "<em>").replaceAll("</[a-zA-Z0-9 =#\"]*>",
							"</em>");
				}
				catchResult.setSummary(summary);
				// 微博链接
				catchResult.setHref(jsoup.text(element, t.rst_href));
				if(mt.getType().equals(SearchTypeEnum.MS_WEIXIN.getSearchType()))
				{

					catchResult.setHref(url);
				}
				// 发布时间
				String pubdate = jsoup.text(element, t.rst_pubdate);
				if(mt.getType().equals(SearchTypeEnum.MS_WEIXIN.getSearchType()))
				{
					pubdate=jsoup.outhtml(element, t.rst_pubdate);
					//System.out.println("test://pubdate/"+pubdate);
//					int index1=pubdate.indexOf("'");
//					int index2=pubdate.lastIndexOf("'");
					//System.out.println("index1:"+index1);
					//System.out.println("index2:"+index2);
//
//					pubdate=pubdate.substring(index1+1,index2);

				}
				//System.out.println("source:"+mt.getSource());
				//System.out.println("type:"+mt.getType());
				//System.out.println("pubdate:"+pubdate);
				if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(mt.getType()))
					parseDate(catchResult, ctm, pubdate);
				else if(SearchTypeEnum.MS_WEIXIN.getSearchType().equals(mt.getType()) )
				{
					pubdate = pubdate.substring(pubdate.indexOf("t=\"")+3,pubdate.lastIndexOf("\""));//更新格式处理
					parseDate(catchResult, ctm, pubdate);
				}
//				else if(SearchTypeEnum.MS_WEBPAGE_BAIDU.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()) )
//				{
//					parseDate(catchResult, ctm, "esou:"+catchResult.getLinks());
//				}
				else if(SearchTypeEnum.MS_WEBPAGE_BAIDU.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()))
				{
					catchResult.setPubdate(pubdate);

				}else if(SearchTypeEnum.MS_WEBPAGE_SOGOU.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()))
				{

					String[] split = catchResult.getInfo().split("-");
					if(split.length>=5) {
						String dat = split[2] + "-" + split[3] + "-" + split[4];
						catchResult.setPubdate(dat.trim());
						catchResult.setInfo(split[0] + "-" + split[1].trim());
					}else{
						catchResult.setPubdate("");
						catchResult.setInfo("");
					}

				}else if(SearchTypeEnum.MS_WEBPAGE_BING.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()))
				{
					if(pubdate.contains(" · ")){
						String[] split=pubdate.split("[ · ]");
						String dat = split[0];
						catchResult.setPubdate(dat.trim());
					}else{
						catchResult.setPubdate("");
					}
				} else if(SearchTypeEnum.MS_WEBPAGE_CHINASO.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()))
				{
					String[] split = catchResult.getLinks().split(" ");
					if(split.length>=4){
						String dat = split[3];
						catchResult.setPubdate(dat.trim());
						catchResult.setInfo(split[0]+split[1]+split[2].trim());
					}else {
						catchResult.setPubdate("");
						catchResult.setInfo("");
					}
				}else if(SearchTypeEnum.MS_NEWS_CHINASO.getSearchType().equals(source) &&SearchTypeEnum.MS_NEWS.getSearchType().equals(mt.getType()))
				{
					String[] split = catchResult.getInfo().split("-");
					if(split.length>=4){
						String dat = split[0]+"-"+split[1]+"-"+split[2];
						catchResult.setPubdate(dat.trim());
						catchResult.setInfo(split[3].trim());
					}else {
						catchResult.setPubdate("");
						catchResult.setInfo("");
					}
				}
				else if(SearchTypeEnum.MS_WEBPAGE_QIHU.getSearchType().equals(source) &&SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(mt.getType()))
				{
					String[] split = pubdate.trim().split("：");
					catchResult.setPubdate(split[1]);
					catchResult.setInfo(info.trim());
				}
				else if(SearchTypeEnum.MS_NEWS_BAIDU.getSearchType().equals(source) &&SearchTypeEnum.MS_NEWS.getSearchType().equals(mt.getType()))
				{
					String[] split = info.split(" ");
					String dat = split[1];
					catchResult.setPubdate(dat.trim());
					catchResult.setInfo(split[0].trim());
				}
				else if(SearchTypeEnum.MS_NEWS_SOGOU.getSearchType().equals(source) &&SearchTypeEnum.MS_NEWS.getSearchType().equals(mt.getType()))
				{
					String[] split = catchResult.getInfo().split(" ");
                    if(split.length>=2){
                        String dat = split[1];
                        catchResult.setPubdate(dat.trim());
                        catchResult.setInfo(split[0]);
                    }
				}
				else if(SearchTypeEnum.MS_NEWS_QIHU.getSearchType().equals(source) &&SearchTypeEnum.MS_NEWS.getSearchType().equals(mt.getType()))
				{
					String[] split = catchResult.getInfo().split("[|]");
					String dat = split[1];
					catchResult.setPubdate(dat.trim());
					catchResult.setInfo(split[0].trim());
				}
				else if(SearchTypeEnum.MS_FORUM_TIANYA.getSearchType().equals(source) &&SearchTypeEnum.MS_FORUM.getSearchType().equals(mt.getType()))
				{
					parseDate(catchResult, ctm, "esou:"+pubdate);

				}
				else if(SearchTypeEnum.MS_WENDA_SOGOU.getSearchType().equals(source) &&SearchTypeEnum.MS_WENDA.getSearchType().equals(mt.getType()))
				{
					String[] split = catchResult.getInfo().split("-");
                    if(split.length>=5) {
                        String dat = split[2] + "-" + split[3] + "-" + split[4];
                        catchResult.setPubdate(dat.trim());
                        catchResult.setInfo(split[0].trim());
                    }

				}else if(SearchTypeEnum.MS_WENDA_BAIDU.getSearchType().equals(source) &&SearchTypeEnum.MS_WENDA.getSearchType().equals(mt.getType()))
				{
					//System.out.println("zhongsou:"+pubdate);
					String[] split = catchResult.getInfo().split(" ");
					if(split.length>=4) {
                        String dat = split[0];
                        catchResult.setPubdate(dat.trim());
                        catchResult.setInfo(split[1] + " " + split[2] + " " + split[3]);
                    }
				}
				else if(SearchTypeEnum.MS_WENDA_QIHU.getSearchType().equals(source) &&SearchTypeEnum.MS_WENDA.getSearchType().equals(mt.getType()))
				{
					//System.out.println("zhongsou:"+pubdate);

					String[] split = info.split(": ");
					if(split.length>=2){
						catchResult.setPubdate(split[1]);
						String[] split1 = split[0].split(" - ");
						if(split1.length>=1)
						catchResult.setInfo(split1[0]);
					}
				}else if(SearchTypeEnum.MS_TIEBA_BAIDU.getSearchType().equals(source) &&SearchTypeEnum.MS_TIEBA.getSearchType().equals(mt.getType()))
				{
					catchResult.setPubdate(pubdate);
					catchResult.setInfo(info);
				}
				else
					catchResult.setPubdate(pubdate);

				//System.out.println("item date:"+catchResult.getPubdate());
				// 微博来源
				String source = jsoup.text(element, t.rst_source);
				parseSource(catchResult, source);
				// 微博转载
				String repeat = jsoup.text(element, t.rst_repeat);
//				 System.out.println("repeat:"+repeat);
				catchResult.setRepeat(repeat.replace("转发","").trim());
				// 微博评论
				String reply = jsoup.text(element, t.rst_reply);
//				System.out.println("reply:"+reply);
				catchResult.setReply(reply.replace("评论","").trim());
				// 媒体信息
				if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(mt.getType()) || SearchTypeEnum.MS_TIEBA.getSearchType().equals(mt.getType())) {
					String media = jsoup.html(element, t.rst_media);
					parseMedia(catchResult, t.name, media);
					catchResult.setInfo(catchResult.getSource());
				}
				if(title==null||title.trim()==""){
					if(!mt.getType().equals(SearchTypeEnum.MS_WEIBO.getSearchType())) {
						continue;
					} else{
						if(summary==null||summary.trim()=="") continue;
					}
				}
				catchResults.add(catchResult);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if (catchResults.size() > 1) {
			if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(mt.getType()))
			// 微博结果排序 (回复数+结果数)
			{
				Collections.sort(catchResults, new CatchResult());

			}
			else {
				// TODO
				// 非微博时，使用时间排序，并对结果排重

			}
			if(catchResults.getTotal()==0)
				catchResults.setTotal(1000000);
		}
		//if(catchResults.size()==0)
		//	catchResults.setTotal(0);
		//	System.out.println(catchResults.size()+"  "+catchResults.getTotal());
		return catchResults;
	}

	/**
	 * 作者头像
	 *
	 * @param catchResult
	 *            存储结果
	 * @param avatar
	 *            头像
	 */
	private void parseAvatar(CatchResult catchResult, String avatar) {
		if (avatar == null || avatar.length() == 0)
			return;
		if (avatar.startsWith("http://")) {
			catchResult.setAvatar(avatar);
			if(avatar.indexOf("qlogo.cn")!=-1)
			{
				catchResult.setAvatar("/images/qq.png");

			}
			return;
		}
		String s = "";
		if (avatar.indexOf("http://") != -1)
			s += avatar.substring(avatar.indexOf("http://") - 1, avatar.indexOf("http://"));
		if ("(".equals(s))
			avatar = avatar.substring(avatar.indexOf("(") + 1, avatar.indexOf(")"));
		catchResult.setAvatar(avatar);
	}

	private  String t3(String key)
	{
		Calendar ca = Calendar.getInstance();
		int num = Integer.parseInt(key.replaceAll("\\D", ""));
		if (key.indexOf("小时前") != -1)
			ca.set(11, ca.get(11) - num);
		else if (key.indexOf("分钟前") != -1)
			ca.set(12, ca.get(12) - num);
		else if (key.indexOf("秒前") != -1)
			ca.set(13, ca.get(13) - num);
		else if (key.indexOf("天前") != -1)
			ca.set(5, ca.get(5) - num);
		return ca.getTimeInMillis()+"";
	}
	private  String t4(String key,String hours)
	{
		Calendar ca = Calendar.getInstance();
		String []num = hours.split(":");
		int []nums=new int[2];
		nums[0]=Integer.parseInt(num[0]);
		nums[1]=Integer.parseInt(num[1]);

		if (key.indexOf("昨天") != -1)
			ca.set(5, ca.get(5) - 1);
		else if (key.indexOf("前天") != -1)
			ca.set(5, ca.get(5) - 2);
		ca.set(11, nums[0]);
		ca.set(12, nums[1]);
		// System.out.println(key+":"+ca.getTimeInMillis());
		return ca.getTimeInMillis()+"";
	}



	private void parseDate(CatchResult catchResult, long t, String pubdate) {
//		System.out.println("待分析时间串："+pubdate);

		if (pubdate == null || pubdate.length() == 0)
			return;
		Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
		Matcher matcher = pattern.matcher(pubdate);

		Pattern pattern0 = Pattern.compile("[0-9]{4}[年|\\-|/][0-9]{1,2}[月|\\-|/][0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}");
		Matcher matcher0 = pattern0.matcher(pubdate);

		Pattern pattern1 = Pattern.compile("[0-9]{4}[年|\\-|/|\\.][0-9]{1,2}[月|\\-|/|\\.][0-9]{1,2}");
		Matcher matcher1 = pattern1.matcher(pubdate);


		Pattern pattern2 = Pattern.compile("\\d{1,2}(小时|分钟|秒|天)前");
		Matcher matcher2 = pattern2.matcher(pubdate);

		Pattern pattern3 = Pattern.compile("(今天|昨天|前天)([0-9]{1,2}:[0-9]{1,2})");
		Matcher matcher3 = pattern3.matcher(pubdate);


		//Pattern pattern3 = Pattern.compile("[\\u4e00-\\u9fa5]+");
		//Matcher matcher3 = pattern.matcher(pubdate);
		// 包含中文时
		if (matcher.find()||pubdate.indexOf("esou")!=-1) {
			//CatchResult.setPubdate(pubdate);
			//return;
			if(matcher0.find())
			{
				pubdate=matcher0.group();
				//	System.out.println("发现匹配时间串"+pubdate);
			}
			else if(matcher1.find())
			{
				pubdate=matcher1.group();
				//System.out.println("发现匹配时间串1"+pubdate);
			}
			else if(matcher2.find())
			{
				pubdate=t3(matcher2.group());
			}
			else if(matcher3.find())
			{
				pubdate=t4(matcher3.group(),matcher3.group(2));
			}
			else
			{
//			System.out.println("时间串过于复杂，无法分析:"+pubdate);
				catchResult.setPubdate(pubdate.replace("esou:", ""));


				return;
			}



		}
		long rq = 0;
		try {
			rq = Long.parseLong((pubdate.length() == 10) ? pubdate + "000" : pubdate);
		} catch (Exception e) {
			if (pubdate.length() == 10) {

				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				try {
					rq = dateFormat.parse(pubdate).getTime();
				} catch (Exception ex) {
					SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy.MM.dd");
					try {
						rq = dateFormat2.parse(pubdate).getTime();
					} catch (Exception ex2) {

					}
				}
			} else if (pubdate.length() == 16) {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				try {
					rq = dateFormat.parse(pubdate).getTime();
				} catch (Exception ex) {

				}
			}
			else
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
				try {
					rq = dateFormat.parse(pubdate).getTime();
				} catch (Exception ex) {

				}
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);

		long todayzero=cal.getTimeInMillis();
		catchResult.setQ(rq);
		catchResult.setQ(rq);
		long i = (t-rq) / 1000;
//System.out.println("longs:"+i);

		long j=(t-todayzero)/1000;
		if (i <= 60) {
			if(i<=0)
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("M月d日 HH:mm");
				catchResult.setPubdate(dateFormat.format(new Date(rq)));
			}
			else
				catchResult.setPubdate(i + "秒前");
		} else if (i <= 60 * 60) {
			catchResult.setPubdate(i / 60 + "分钟前");
		} else if (i <= j) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
			catchResult.setPubdate("今天 " + dateFormat.format(new Date(rq)));
		} else if (i <= 24 * 60 * 60+j) {




			SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

			catchResult.setPubdate("昨天 " + dateFormat.format(new Date(rq)));
		} else {
			//System.out.println("提取到时间:"+rq);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日 HH:mm");
			catchResult.setPubdate(dateFormat.format(new Date(rq)));
		}
	}

	/**
	 * 解析来源
	 *
	 * @param catchResult
	 *            存储结果
	 * @param source
	 *            来源
	 */
	private void parseSource(CatchResult catchResult, String source) {
		if (source == null)
			return;
		if (source.contains("来自"))
			source = source.substring(source.indexOf("来自") + 2);
		catchResult.setSource(source);
	}

	/**
	 * 解析媒体信息，图片或新闻
	 *
	 * @param catchResult
	 *            保存解析结果的类
	 * @param name
	 *            微博名称或百度贴吧
	 * @param media
	 *            html 格式媒体信息
	 */
	private void parseMedia(CatchResult catchResult, String name, String media) {
		if (catchResult == null || name == null || media == null)
			return;
		Document doc = Jsoup.parse(media);
		if (doc == null)
			return;
		if (SearchTypeEnum.MS_WEIBO_SINA.getSearchType().equals(name))
			parseMediaForSina(catchResult, doc);
		else if (SearchTypeEnum.MS_TIEBA_BAIDU.getSearchType().equals(name)) {
			parseMediaForBaidu(catchResult, doc);
		}
	}

	/**
	 * 解析新浪微博媒体信息
	 */
	private void parseMediaForSina(CatchResult catchResult, Document doc) {
		// 图片集合
		Elements mdNodes = doc.getElementsByTag("img");
		for (int i = 0; i < mdNodes.size(); i++) {
			Element imgNode = mdNodes.get(i);
			String actionType = imgNode.attr("action-type");
			String  actionType2 = imgNode.parent().attr("action-type");
			if ("fl_pics".equals(actionType)) {
				catchResult.getImgs().add(imgNode.attr("src"));
			} else if ("feed_list_media_img".equals(actionType)) {
				catchResult.setImg(imgNode.attr("src"));
			} else if ("feed_list_third_video".equals(actionType2)) {
				String video = imgNode.parent().attr("action-data");
				catchResult.setVdpic(imgNode.attr("src"));
				catchResult.setVideo(video.substring(video.lastIndexOf("&full_url=") + 10).replaceAll("%2F", "/")
						.replaceAll("%3A", ":"));
			}
		}
	}

	/**
	 * 解析百度贴吧媒体信息
	 */
	private void parseMediaForBaidu(CatchResult catchResult, Document doc) {
		try {
			Elements mdNodes = doc.getElementsByClass("p_mediaCont");
			List<String> imgs = new ArrayList<String>();
			for (Element e : mdNodes) {
				String src = e.getElementsByClass("p_pic").first().attr("src");
				imgs.add(src);
			}
			if (imgs.size() == 1)
				catchResult.setImg(imgs.get(0));
			else if (imgs.size() > 1)
				catchResult.setImgs(imgs);
		} catch (Exception e) {

		}
	}

}
